/**
 * 借款单据前台页面逻辑 
 */
 
function getDjlx(){
	return "jk";
}
function getHeadDs(){
	return getDataset("jk_jkzb");
}
function getSubDs(){
	return getDataset("jk_items");
}

/**
 * 初始化
 */ 
function initJkBx() {
	//billUI.eventHandler.onBeforeBoLineIns = billUI.eventHandler.onBeforeBoLineAdd;
	var headDs = getDataset('jk_jkzb');
	
	//行选中状态变换，undo情况下，都需要更新按钮状态
    if(headDs != null){
		/*	headDs.onAfterRowSelect = function(event){//设置按钮的状态
			//如果是多行选中，则此处不设置业务状态，由getUserStateMap()来返回可控制的状态
			var indices = this.getSelectedIndices();
			if(indices.length > 1){
				ButtonManager.getInstance().setBusinessStatus(null);
				ButtonManager.getInstance().setOperateStatus(IBillOperate.OP_NOTEDIT);
				ButtonManager.getInstance().updateButtons();
			}else{
				var value=null,state=null;
				if(this.getRowCount() != 0){
		   			var row = event.currentRow;
		   			if(row != null){
		   				var billStatus = billUI.billController.billStatusField;
		   				value = event.currentRow.getCellValue(this.nameToIndex(billStatus))
		   			}
				}
				if(value != null)
					state = IBillOperate.OP_NOTEDIT;
	   			billUI.setTotalUIStatus(state, value);	
			}
		}
		headDs.onAfterRowUnSelect = function(event){//设置按钮的状态
			//如果是多行选中，则此处不设置业务状态，由getUserStateMap()来返回可控制的状态
			var indices = this.getSelectedIndices();
			if(indices.length == 1){
				var value=null,state=null;
	   			var row = this.getSelectedRow();
	   			if(row != null){
	   				var billStatus = billUI.billController.billStatusField;
	   				value = row.getCellValue(this.nameToIndex(billStatus))
	   			}
				if(value != null)
					state = IBillOperate.OP_NOTEDIT;
	   			billUI.setTotalUIStatus(state, value);	
			}else{
				ButtonManager.getInstance().setBusinessStatus(null);
				ButtonManager.getInstance().setOperateStatus(IBillOperate.OP_NOTEDIT);
				ButtonManager.getInstance().updateButtons();
			}
		}*/
    }
	//摘要的參照框不需要派發事件
	var zyComp = getComponent('headTab_jkzb_form').getElement('zy_mc'); 
	if(zyComp != null){
		// innerProcessEnter失去焦点时如果当前值和oldValue不等,则执行向后台发请求逻辑，此处不触发
    	zyComp.innerProcessEnter = function(){
    		var zy_mc = zyComp.getValue();
    		headDs.setValueAt(headDs.getSelectedIndex(),headDs.nameToIndex('zy'),zy_mc);
    	}
    }
    	//如果选中的tab页发生变换，需要更新按钮状态
	getComponent("bodyTab").afterActivedTabItemChange = function(currItem) {
		ButtonManager.getInstance().updateButtons();
	};
	  
	/*单据执行“增加”动作后执行*/ 
	billUI.eventHandler.onAfterBoAdd = function(newRow)
	{
		//因为从首页进入没有设置常用单据，所以去掉
		/*
		if(getParameter("approveid") != null)
			return;
		*/
		var cydjArrStr = getSessionAttribute("cydj");
		if(cydjArrStr == null)
			return;
		var cydjArr = eval(cydjArrStr);
		var jkitemDs = getDataset("jk_items");
		for(var i = 0,len = cydjArr.length; i < len; i ++){
			var row = jkitemDs.getEmptyRow();
			row.cydj = true;
			//根据常用单据设置值
			for(var j = 0; j < jkitemDs.metadata.length; j ++){
				var value = cydjArr[i][jkitemDs.metadata[j].key];
				if(value != null)
					row.setCellValue(j, value);
			}
			jkitemDs.addRow(row);
		}
	};
	/*单据执行“编辑”动作后执行
	 */
	billUI.eventHandler.onAfterBoEdit = function(newRow)
	{
		//修改外币单据，本币汇率可编辑
		var jtbwb = getSessionAttribute("jtbwb");
		var bbhlEle = getComponent("headTab_jkzb_form").getElement("bbhl");
		var bzbm = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('bzbm'));
		if(jtbwb != bzbm){
			if(bbhlEle != null) 
				bbhlEle.setActive(true);
		}
	};
	
	headDs.onAfterDataChange = function(event){
		/*币种变化，则汇率变化,汇率可写；原币可写*/
		var ybArr = ['ybje','ybye','hkybje','zfybje','cjkybje'];
		var bbArr = ['bbje','bbye','hkbbje','zfbbje','cjkbbje'];
		if(event.cellColIndex == this.nameToIndex("bzbm")){
			if(event.currentValue == event.oldValue)
				return;
			reset4Bzbm(this, event.currentValue, "jk");
		}
		//主表中每个原币改变，都要触发相应的本币
		
		for(var k = 0,len = ybArr.length; k < len; k++){
			if(event.cellColIndex == this.nameToIndex(ybArr[k])){
 				var bbIndex = this.nameToIndex(bbArr[k]);
 				var bbhl = event.currentRow.getCellValue(headDs.nameToIndex('bbhl'));
				headDs.setValueAt(event.cellRowIndex, bbIndex, parseFloat(event.currentValue)*parseFloat(bbhl));
				/*借款表头原币金额-->表头余额*/
				if(ybArr[k] == 'ybje')
					headDs.setValueAt(event.cellRowIndex, headDs.nameToIndex("ybye"), event.currentValue);
			}
		}
		
		/*汇率变化*/
		if(event.cellColIndex == this.nameToIndex("bbhl")){
			resetJEforHl('jk');
		}
		
		/*表头的收支项目变化，则表体对应收支改*/
		if(event.cellColIndex == this.nameToIndex('szxmid')){
			if(isSave){//保存操作后不改变收支项目
				isSave = false;
				return;
			}
			var busDs = getDataset('jk_items');
			var count = busDs.getRowCount(); 
			for(var i=0; i < count; i++){
				var index = busDs.nameToIndex('szxmid');
				busDs.setValueAt(i,index,event.currentValue,true);
			}
		}
		//录入报销人，应自动带出报销部门，如果报销人公司与费用承担公司相同，应自动带出费用承担部门。
		/*
		if(event.cellColIndex == this.nameToIndex('deptid')){
			var fydwbm = event.currentRow.getCellValue(this.nameToIndex('fydwbm'));
			var zfdwbm = event.currentRow.getCellValue(this.nameToIndex('zfdwbm'));
			if(fydwbm == zfdwbm)
				this.setValueAt(event.cellRowIndex,this.nameToIndex('fydeptid'),event.currentValue,true);
		}
		*/
		//表头报销人变化
		if(event.cellColIndex == this.nameToIndex('jkbxr')){
			//重新获取个人银行账号
			var strs = getService("webbx_common_service").getFromJkbxrChange(event.currentValue);
			if(strs == null){
				this.setValueAt(this.getSelectedIndex(),this.nameToIndex('skyhzh'),null,false);
				this.setValueAt(this.getSelectedIndex(),this.nameToIndex('skyhzh_mc'),null,false);
			}else{
				var arr = eval(strs);
				var row = this.getSelectedRow();
				this.setValueAt(event.cellRowIndex,this.nameToIndex('skyhzh'),arr[1],false);
				this.setValueAt(event.cellRowIndex,this.nameToIndex('skyhzh_mc'),arr[2],false);
				
				/*
				报销部门和费用承担部门都应该随之变成报销人所在部门。
				注意如果费用承担公司与报销公司不一致，费用承担部门不要变。
				*/
				this.setValueAt(event.cellRowIndex,this.nameToIndex('deptid'),arr[0],true);
		
				var dwbm = event.currentRow.getCellValue(this.nameToIndex("dwbm"));
				var fydwbm = event.currentRow.getCellValue(this.nameToIndex("fydwbm"));
				if(dwbm != null && fydwbm != null && dwbm == fydwbm){
					this.setValueAt(event.cellRowIndex,this.nameToIndex('fydeptid'),arr[0],true);
				}
			}
		}
	}
	var busDs = getDataset('jk_items');
	if(busDs != null)
		$initJkBusItems(headDs,busDs);
}

function $initJkBusItems(headDs,busDs){
	busDs.onBeforeRowInsert = function(rowIndex, rows){
		jkbxBeforeBoLineAdd(rows[0], this);
	}
	busDs.onAfterDataChange = function(event, pageIndex){	
		var amountIndex = this.nameToIndex("amount");
		if(event.cellColIndex == amountIndex){
		
		
			var pre = null;
			if(window.bzbmmap != null){
				pre = window.bzbmmap.get(headDs.getSelectedRow().getCellValue(headDs.nameToIndex('bzbm')));
			}
			if(pre == null)
				pre = getSessionAttribute("precision");
			var newSum = this.totalSum(["amount"], null, null, pre);
			setHeadDsValue(newSum[0]);
		}
	}
};
/**
 * 根据子表的【amount】变化来触发
 * */
function setHeadDsValue(newSum){
	var headDs = getDataset('jk_jkzb');
	headDs.setValueAt(headDs.getSelectedIndex(), headDs.nameToIndex("ybje"), newSum);
//	headDs.setValueAt(headDs.getSelectedIndex(), headDs.nameToIndex("total"), newSum);
	headDs.setValueAt(headDs.getSelectedIndex(), headDs.nameToIndex("ybye"), newSum);
}

function adjustSpliterSelf(billUI) {
	try{
		var bodyHeight = document.body.offsetHeight;
		if(billUI.currentPanel == ManageUI.LISTPANEL){
			var headGrid = getComponent("headTab_jkzb_grid");
			var goldHeight = (bodyHeight - 50) * 0.42;
			$('toptr').style.height = goldHeight;
		}
		else{
			var headTab = getComponent("headTabCard");
			var height = 0;
			if(headTab != null && headTab.tabItems.length > 1){
				height += 26;
			}
			var itemName = headTab.tabItems[0].name;
			var headForms = getComponents(AutoFormComp);
			//以第一个选中项的所有表单和为准，多于一个的表单为fieldset组织，额外加上20px；
			for(var i = 0; i < headForms.length; i ++){
				if(headForms[i].id.startWith("headTab_")){
					height += headForms[i].getHeight();
					if(i > 0)
						height += 20;
				}
			}
			//var headForm = getComponent("headTab_jkzb_form");
			//height += headForm.getHeight();
			var goldHeight = (bodyHeight - 50) * 0.42;
			if(height > goldHeight){
				$('toptr').style.height = goldHeight;
			}
			else
				$('toptr').style.height = height;
				
			var tailForm = getComponent("tailTab_tail_form");
			if(tailForm != null){
				$('tailtr').style.height = tailForm.getHeight();
			}
		}
	}
	catch(error){
		log("error occurred while adjusting spliter");
	}
};

/*复制新增后，处理*/
BillEventHandler.prototype.onAfterBoCopyAdd = function(row)
{
	
}

